package StructuralPattern.Composite.TransparentMode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname : Component
 * @Description : 抽象组件
 * @Author : chentianyu
 * @Date 2023/2/19 14:48
 */


public abstract class Component {

    private String info;  // 信息
    private Component parent;  // 父节点
    private List<Component> children;  // 子节点集合

    public Component(String info) {
        this.info = info;
        this.children = new ArrayList<>();
    }

    public void setParent(Component parent) {
        this.parent = parent;
    }

    public void addChild(Component child) {
        child.setParent(this);
        this.children.add(child);
    }

    public void getInfo() {
        System.out.println("—————————打印节点信息————————");
        System.out.println("1、本节点: " + this.info);
        if (this.parent == null) {
            System.out.println("2、父节点：无父节点，为根节点");
        } else {
            System.out.println("2、父节点：" + this.parent.info);
        }
        if (this.children.isEmpty()) {
            System.out.println("3、子节点：无子节点，为叶子节点");
        } else {
            System.out.println("3、子节点：");
            for (Component child : this.children) {
                System.out.println("   " + child.info);
            }
        }
        System.out.println();
    }

}
